package com.cavy.platform.server.mapper;

import com.cavy.platform.server.domain.GitRemotePipeline;
import com.cavy.platform.server.domain.Pipeline;
import com.cavy.platform.server.dto.GitRemotePipelineCreateDto;
import com.cavy.platform.server.dto.PipelineCreateDto;
import com.cavy.platform.server.dto.PipelineDeleteDto;
import com.cavy.platform.server.dto.PipelineDto;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

/**
 * @author 唐旭
 * @Date 2018/7/17
 */
@Mapper
public interface PipelineMapper {
    PipelineMapper INSTANCE = Mappers.getMapper(PipelineMapper.class);

    default Pipeline convert(PipelineCreateDto dto) {
        if (dto instanceof GitRemotePipelineCreateDto) {
            return GitRemotePipelineMapper.INSTANCE.convert((GitRemotePipelineCreateDto) dto);
        }
        return standardConvert(dto);
    }

    @Mapping(target = "project.name", source = "projectName")
    @Mapping(target = "project.workSpace.name", source = "workspace")
    Pipeline standardConvert(PipelineCreateDto dto);

    @Mapping(target = "project.name", source = "projectName")
    @Mapping(target = "project.workSpace.name", source = "workspace")
    Pipeline convert(PipelineDeleteDto dto);

    default PipelineDto toDto(Pipeline pipeline) {
        if (pipeline instanceof GitRemotePipeline) {
            return GitRemotePipelineMapper.INSTANCE.toDto((GitRemotePipeline) pipeline);
        }

        return toStandardDto(pipeline);
    }

    @Mapping(target = "project.workSpace", source = "project.workSpace.name")
    PipelineDto toStandardDto(Pipeline pipeline);
}
